package org.zty.common;

import org.zty.common.BaseService;
import org.zty.db.DBMgr;
import org.zty.util.ZException;

import com.ibatis.sqlmap.client.SqlMapClient;

public class SeqService extends BaseService {
	public SeqService() {
		this.client = DBMgr.getSqlMapClient();
	}

	public SeqService(SqlMapClient client) {
		this.client = client;
	}

	public synchronized int getNextId(String name) throws ZException {
		try {
			Sequence seq = new Sequence(name, -1);
			seq = (Sequence) this.client.queryForObject("getSeq", seq);
			if (seq == null) {
				throw new Exception("获取序号失败：" + name);
			}
			Object parameterObject = new Sequence(name, seq.getKeyValue() + 1);
			this.client.update("updateSeq", parameterObject);
			return seq.getKeyValue();
		} catch (Exception e) {
			throw new ZException(e);
		}
	}
}